package com.javatom.notes.common.config.redis;

import com.javatom.notes.common.handler.DistributedLockHandler;
import com.javatom.notes.common.util.RedissonLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @description: 分布事锁配置
 * @author: 曾维录
 * @time: 2019/7/29 15:08
 */
@Configuration
@ConditionalOnProperty(prefix = "redisson.server", name = "enabled", havingValue = "true")
@Slf4j
public class RedissonConfig {

    @Resource
    private RedissonProperties redissonProperties;
    @Bean
    public DistributedLockHandler distributedLockHandler(){
        return new DistributedLockHandler();
    }
    /**
     *
     * @param redissonProperties redissonProperties
     * @return RedissonManager
     */
    @Bean
    @Order(value = 1)
    public RedissonManager redissonManager(RedissonProperties redissonProperties) {
        RedissonManager redissonManager =
                new RedissonManager(redissonProperties);
        log.info("[RedissonManager]组装完毕,当前连接方式: {}"
                + ",连接地址: {}", redissonProperties.getType(), redissonProperties.getAddress());
        return redissonManager;
    }

    /**
     *
     * @param redissonManager redissonManager
     * @return RedissonLockUtil
     */
    @Bean
    @Order(value = 2)
    public RedissonLockUtil redissonLockUtil(RedissonManager redissonManager) {
        RedissonLockUtil redissonLock = new RedissonLockUtil(redissonManager);
        log.info("[RedissonLock]组装完毕");
        return redissonLock;
    }
}
